package cn.patient.pp.module.knowledge.controller.admin;

import cn.patient.pp.module.knowledge.dal.dataobject.KnowledgeBaseDO;
import cn.patient.pp.module.knowledge.service.KnowledgeBaseService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.patient.pp.framework.common.pojo.PageParam;
import cn.patient.pp.framework.common.pojo.PageResult;
import cn.patient.pp.framework.common.pojo.CommonResult;
import cn.patient.pp.framework.common.util.object.BeanUtils;
import static cn.patient.pp.framework.common.pojo.CommonResult.success;

import cn.patient.pp.framework.excel.core.util.ExcelUtils;

import cn.patient.pp.framework.apilog.core.annotation.ApiAccessLog;
import static cn.patient.pp.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.patient.pp.module.knowledge.controller.admin.vo.*;

/**
 * @author liqingsong
 * @version 1.0
 * Create by 2025/4/14
 */
@Tag(name = "管理后台 - 知识库")
@RestController
@RequestMapping("/business/knowledge-base")
@Validated
public class KnowledgeBaseController {
    @Resource
    private KnowledgeBaseService knowledgeBaseService;

    @PostMapping("/create")
    @Operation(summary = "创建知识库")
    @ApiAccessLog(operateType = CREATE)
    @PreAuthorize("@ss.hasPermission('bussiness:knowledge-base:create')")
    public CommonResult<Integer> createKnowledgeBase(@Valid @RequestBody KnowledgeBaseSaveReqVO createReqVO) {
        return success(knowledgeBaseService.createKnowledgeBase(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新知识库")
    @ApiAccessLog(operateType = UPDATE)
    @PreAuthorize("@ss.hasPermission('bussiness:knowledge-base:update')")
    public CommonResult<Boolean> updateKnowledgeBase(@Valid @RequestBody KnowledgeBaseSaveReqVO updateReqVO) {
        knowledgeBaseService.updateKnowledgeBase(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除知识库")
    @ApiAccessLog(operateType = DELETE)
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('bussiness:knowledge-base:delete')")
    public CommonResult<Boolean> deleteKnowledgeBase(@RequestParam("id") Integer id) {
        knowledgeBaseService.deleteKnowledgeBase(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得知识库")
    @ApiAccessLog(operateType = GET)
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('bussiness:knowledge-base:query')")
    public CommonResult<KnowledgeBaseRespVO> getKnowledgeBase(@RequestParam("id") Integer id) {
        KnowledgeBaseDO knowledgeBase = knowledgeBaseService.getKnowledgeBase(id);
        return success(BeanUtils.toBean(knowledgeBase, KnowledgeBaseRespVO.class));
    }

    @PostMapping("/page")
    @Operation(summary = "获得知识库分页")
    @ApiAccessLog(operateType = GET)
    @PreAuthorize("@ss.hasPermission('bussiness:knowledge-base:query')")
    public CommonResult<PageResult<KnowledgeBaseRespVO>> getKnowledgeBasePage(@Valid @RequestBody KnowledgeBasePageReqVO pageReqVO) {
        PageResult<KnowledgeBaseRespVO> pageResult = knowledgeBaseService.getKnowledgeBasePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, KnowledgeBaseRespVO.class));
    }

//    @GetMapping("/export-excel")
//    @Operation(summary = "导出知识库 Excel")
//    @PreAuthorize("@ss.hasPermission('bussiness:knowledge-base:export')")
//    @ApiAccessLog(operateType = EXPORT)
//    public void exportKnowledgeBaseExcel(@Valid KnowledgeBasePageReqVO pageReqVO,
//                                         HttpServletResponse response) throws IOException {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        List<KnowledgeBaseRespVO> list = knowledgeBaseService.getKnowledgeBasePage(pageReqVO).getList();
//        // 导出 Excel
//        ExcelUtils.write(response, "知识库.xls", "数据", KnowledgeBaseRespVO.class,
//                BeanUtils.toBean(list, KnowledgeBaseRespVO.class));
//    }
}

